﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;

public partial class iframe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["sTittle"] = "远程教育平台";
        if (Membership.GetUser() == null) //如果用户名为空，返回登录界面
        {
            Response.Write(string.Format("<script language='javascript'>alert('{0}')</script>", "系统超时，请重新登录"));
            Response.Redirect("index.aspx",true);
        }
        string x = Membership.GetUser().UserName.TrimEnd();

        string[] y = Roles.GetRolesForUser(x);
        Session["sUserName"] = x;
        Session["sRole"] = y[0];

        string zsql = "select jigou,kecheng,kechenged,voted,username1 from account where username='" + Session["sUserName"] + "'";
        SqlDataReader Reader = DbHelper.ExecuteReader(CommandType.Text, zsql, null);

        //学员：报名参加的课程，已学完的课程，已投票的课程
        //教师：正在进行的课程（终审完成后计入）
        //机构：所有开设的课程（终审完成后计入）
        Reader.Read();
        Session["sJigou"] = Reader.GetValue(0).ToString();
        Session["sKecheng"] = Reader.GetValue(1).ToString();
        Session["sKechenged"] = Reader.GetValue(2).ToString();
        Session["sKecheng_Voted"] = Reader.GetValue(3).ToString();
        Session["sUserName1"] = Reader.GetValue(4).ToString();
        Reader.Close();

        string zdate = DateTime.Now.ToString();
        string zuserip = Request.UserHostAddress.ToString();
        string zsql1 = "insert into details (user_ip,jigou,role,username,username1,xtsj,detail) values ('" + zuserip + "','" + Session["sJigou"].ToString() + "','" + Session["sRole"].ToString() + "','" + Session["sUserName"].ToString() + "','" + Session["sUserName1"].ToString() + "','" + zdate + "','用户登录')";
        DbHelper.ExecuteNonQuery(CommandType.Text, zsql1, null);

        if (!IsPostBack)
        {
            getBgImg();
        }
    }
    /// <summary>
    /// 获取自定义背景图片
    /// </summary>
    /// <returns></returns>
    public string getBgImg()
    {
        string sql = string.Format("select BeiJing from JiGouPeiZhi where JiGouMingCheng='{0}' and beijingqiyong=1", Session["sJiGou"].ToString());
        DataTable dt = DbHelper.ExecuteTable(CommandType.Text, sql, null);
        if (dt.Rows.Count == 1)
        {
            string imgPath = dt.Rows[0]["BeiJing"].ToString();
            if (!File.Exists(MapPath(imgPath)))
            {
                return "images/bg.jpg";
            }
            else
            {
               return  imgPath;
            }
        }
        else
        {
            return  "images/bg.jpg";
        }
    }
}
